﻿namespace Sidvall.Data
{
    public static class DataRowContextManager
    {
        #region Public Members

        #region GetDataRowState

        public static DataRowState GetDataRowState(IDataRowContextProvider item)
        {
            if (item == null)
                throw new System.ArgumentNullException("item");

            if (item.DataRowStateIsDeleted)
                return DataRowState.Deleted;
            else if (item.DataRowStateIsNew)
                return DataRowState.Added;
            else if (item.DataRowStateIsSelfDirty)
                return DataRowState.Modified;
            return DataRowState.Unchanged;
        }

        #endregion
        #region GetDataRowStateResolved

        public static DataRowState GetDataRowStateResolved(IDataRowContextProvider item)
        {
            if (item == null)
                throw new System.ArgumentNullException("item");

            if (item.DataRowStateIsDeleted)
                return DataRowState.Deleted;
            else if (item.DataRowStateIsNew)
                return DataRowState.Added;
            else if (item.DataRowStateIsDirty)
                return DataRowState.Modified;
            return DataRowState.Unchanged;
        }

        #endregion
        #region IsValid

        public static bool IsValid(IDataRowContextProvider item, DataRowFilters rowFilter)
        {
            if (item == null)
                return false;
            if (item.DataRowStateIsDeleted)
            {
                if ((rowFilter & DataRowFilters.Deleted) == DataRowFilters.Deleted)
                    return true;
                return false;
            }
            if (item.DataRowStateIsNew)
            {
                if ((rowFilter & DataRowFilters.Added) == DataRowFilters.Added)
                    return true;
                return false;
            }
            if (item.DataRowStateIsDirty)
            {
                if ((rowFilter & DataRowFilters.Updated) == DataRowFilters.Updated)
                    return true;
                return false;
            }
            else
            {
                if ((rowFilter & DataRowFilters.Unchanged) == DataRowFilters.Unchanged)
                    return true;
                return false;
            }
        }

        #endregion

        #endregion
    }
}
